var selectedId;
var tableRow = $('table tr')

if (typeof jQuery !== 'undefined') {
    (function ($) {
        $('#spinner').ajaxStart(function () {
            $(this).fadeIn();
        }).ajaxStop(function () {
            $(this).fadeOut();
        });
    })(jQuery);
}

function deleteData(url) {
    window.location.href = url + '/' + selectedId

}

function select(id) {
    $('table tr').removeClass('colored');
    $("#table" + id).addClass('colored');
    selectedId = id;
}

$('td').click(clickTd);

var lastUpdatedField;

function clickTd(event) {
    if(event.currentTarget==null){
        event.currentTarget.innerHTML = '<input onkeypress="submit()" size="' + content
            .length + '" type="text" value="' + content + '" >'
    }else
    if (event.currentTarget != lastUpdatedField &&
        event.currentTarget.className != 'readOnly') {

        if (lastUpdatedField != undefined) {
            setToTextField()
        }

        var content = event.currentTarget.innerText
        event.currentTarget.innerHTML = '<input onkeypress="submit()" size="' + content
            .length + '" type="text" value="' + content + '" >'
        lastUpdatedField = event.currentTarget
    }


}
function addRows() {
//    $("#show").find('tbody')
//        .append($())
    $('#show tr:last').after('<tr >' +
        '<td height="25 px"></td>' +
        '<td></td>' +
        '<td></td>' +
        '<td></td>' +
        '<td></td>' +
        '<td></td>' +
        '<td></td>' +
        '</tr>')
}

function setToTextField() {
    if (lastUpdatedField.firstChild.value != undefined) {
        lastUpdatedField.innerHTML = lastUpdatedField.firstChild.value
    }
}
function submit(e) {
    if (!e) e = window.event;
    var keyCode = e.keyCode || e.which;
    if (keyCode == '13') {
        setToTextField();

        var parent = lastUpdatedField.parentNode
        var children = parent.children

        var person = {
            id: children[0].innerText,
            name: children[1].innerText,
            age: children[2].innerText,
            city: children[3].innerText,
            street: children[4].innerText,
            number: children[5].innerText}

        $.ajax("<g:createLink action='updatePersonAJAX'/>", {data: {person: JSON.stringify(person)},
//               // Читаем текстовый ответ сервера
            complete: function (data) {
                $('#personTable').html(data.responseText)
            }})
    }

}